package com.example.myapplication.ucrop;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

public class FourRoundView extends View {
    private Paint mPaint;
    private Paint mPaint2;
    private int mWidth;
    private int mHeight;
    private float mStrokeWidth;
    private float mLineWidth;
    private int mLineColor;


    public FourRoundView(Context context) {
        this(context, null);
    }

    public FourRoundView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FourRoundView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public FourRoundView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mLineWidth =  dip2px(20);
        mStrokeWidth = dip2px(1.5f);
        mLineColor =  Color.WHITE;

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(mLineColor);
        mPaint.setStrokeWidth(mStrokeWidth);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    /**
     * <a href="http://stackoverflow.com/questions/15309029/android-paint-stroke-width-positioning">the stoke is always centered</a> <br>
     * 这里不用0,用的<code>mStrokeWidth / 2F</code>替换0;原因请看上面的链接
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 左上角
        canvas.drawLine(0, mStrokeWidth / 2F, mLineWidth, mStrokeWidth / 2F, mPaint);
        canvas.drawLine(mStrokeWidth / 2F, mStrokeWidth / 2F, mStrokeWidth / 2F, mLineWidth, mPaint);
        // 左下角
        canvas.drawLine(mStrokeWidth / 2F, mHeight - mLineWidth, mStrokeWidth / 2F, mHeight, mPaint);
        canvas.drawLine(mStrokeWidth / 2F, mHeight - mStrokeWidth / 2F, mLineWidth, mHeight - mStrokeWidth / 2F, mPaint);
        // 右上角
        canvas.drawLine(mWidth - mLineWidth, mStrokeWidth / 2F, mWidth, mStrokeWidth / 2F, mPaint);
        canvas.drawLine(mWidth - mStrokeWidth / 2F, mStrokeWidth / 2F, mWidth - mStrokeWidth / 2F, mLineWidth, mPaint);
        // 右下角
        canvas.drawLine(mWidth - mStrokeWidth / 2F, mHeight - mLineWidth, mWidth - mStrokeWidth / 2F, mHeight, mPaint);
        canvas.drawLine(mWidth - mLineWidth, mHeight - mStrokeWidth / 2F, mWidth, mHeight - mStrokeWidth / 2F, mPaint);
    }

    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     *
     * @param dipValue
     * @return
     */
    public int dip2px(float dipValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}